<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/7
 * Time: 15:20
 */
//控制器类
namespace Controller;



use Model\IndexModel;

class MemberController extends TooController{


    //订单管理
    public function order(){
        $model=new IndexModel();
        //查出所有的订单
        $order=$model->pdo_sel("select o.*,m.email from sh_order as o join sh_member as m on o.m_id = m.id  order by id desc",2);
        $this->view->assign("arr",$order);
        $this->view->display();
    }


    //订单操作
    public function order_upd(){
        $model=new IndexModel();
        $type=$_GET['type'];
        $id=$_GET['id'];
        if($type=='fh'){
            //发货操作
            $arr=array();
            $arr['wl_h']=$_GET['wl_h'];
            $arr['wl_n']=$_GET['wl_n'];
            $arr['stat']='4';
            if($model->pdo_upd("sh_order",$arr," where id = $id")){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }
        else if($type==2){
            //付款
            if($model->pdo_upd("sh_order",array('stat'=>2,'pay'=>'后台付款')," where id = $id")){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }else if($type=='5'){
            //收货
            if($model->pdo_upd("sh_order",array('stat'=>5)," where id = $id")){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }else if($type==7){
            //退款
            $order=$model->pdo_sel("select * from sh_order where id = $id");
            if($model->pdo_upd("sh_order",array('stat'=>7)," where id = $id")){
                $member=$model->pdo_sel("select * from sh_member where id = {$order['m_id']}");
                //退还金钱
                $model->pdo_upd("sh_member",array('money'=>$member['money']+$order['money2'])," where id = {$member['id']}");

                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }
    }
    public function member(){
        $arr=$this->model->pdo_sel("select *from sh_member",2);

        $this->view->assign('arr',$arr);
        $this->view->assign('id',$arr['id']);
//        var_dump($arr);exit;
        $this->view->display();
    }
    public function sors(){
//
        if($_GET['type']=='start'){
//            echo "update sh_member set stat=2 where id={$_GET['id']}";exit;
            if($this->model->pdo_exec("update sh_member set stat=1 where id={$_GET['id']}")){
                $this->retuAjax(array('type'=>'1','msg'=>'操作成功'));
            }
        }
        if($_GET['type']=='stop'){
            if($this->model->pdo_exec("update sh_member set stat=2 where id={$_GET['id']}")){
                $this->retuAjax(array('type'=>'1','msg'=>'操作成功'));
            }
        }
    }
    //用户删除
    public function del (){
        $model=new IndexModel();
        $is=$model->pdo_del('sh_member'," where id={$_GET['id']}");
        if($is){
            $this->retuAjax(array('type'=>1,'msg'=>'会员删除成功'));
        }else{
            $this->retuAjax(array('type'=>2,'msg'=>'会员删除失败'));
        }

    }
    //修改密码
    public function change_password (){
        $model=new IndexModel();
        if($id=$_GET['id']){
            $arr=$model->pdo_sel("select *from sh_member where id=$id");
            $this->view->assign('arr',$arr);
        };

        if($_POST['newpassword']){
            $is=$model->pdo_upd('sh_member',array('pass'=>md5($_POST['newpassword']))," where id={$_POST['id']}");
            if($is){
                $this->success('修改成功','Member','change_password');
            }else{
                $this->success('修改失败','Member','change_password');
            }
        }


        $this->view->display();
    }
    //会员编辑
    public function member_upd(){

        $model=new IndexModel();
        if($id=$_GET['id']){
            $arr=$model->pdo_sel("select *from sh_member where id=$id");
            $this->view->assign('arr',$arr);
        };
        if($_POST['type1']=='upd'&&$_POST['id']){
            $arr=array();
//            var_dump($_POST);exit;
            $arr['email']=$_POST['email'];
            $is=$model->pdo_upd('sh_member',$arr," where id={$_POST['id']}");
            if($is){
                $this->redirect('Member','member_add');
            }else{
                $this->redirect('Member','member_add');
            }
        }
        $this->view->display();
    }
    public function member_add(){

        if($_POST['type']=='new'){

            $arr = array();
            $arr['email'] = $_POST['email'];
            $arr['pass'] = md5($_POST['pass']);
            $arr['time'] = time();
            $is = $this->model->pdo_insert('sh_member', $arr);
            if ($is) {
                $this->redirect('Member', 'member');
            } else {
                $this->redirect('Member', 'member');
            }
        }
        $this->view->display();
//        var_dump($_POST);exit;
    }
}